package com.dwxt.mall.service;

import com.dwxt.common.base.YesOrNo;
import com.dwxt.mall.dao.ProductMapper;
import com.dwxt.mall.entity.Product;
import com.dwxt.mall.entity.ProductCategory;
import com.dwxt.mall.entity.ProductExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductAttrService productAttrService;

    @Autowired
    private AsyncService asyncService;

    public List<Product> selectProductListByType(String type) {
        return productMapper.selectMallIndex(type);
    }

    public List<Product> selectModuleProductList(Product product) {
        ProductExample example = new ProductExample();
        example.setOrderByClause("priority desc,create_time asc");
        example.createCriteria().andCategoryIdEqualTo(product.getCategoryId()).andModuleEqualTo(product.getModule()).andIsShelfEqualTo(YesOrNo.YES);
        List<Product> productList = productMapper.selectByExampleWithBLOBs(example);
        return productList;
    }

    public Product selectProductByPrimaryKey(Integer productId) {
        return productMapper.selectByPrimaryKey(productId);
    }

    public int updateProductStock(Product product) {
        ProductExample example = new ProductExample();
        example.createCriteria().andIdEqualTo(product.getId()).andVersionEqualTo(product.getVersion());
        product.setVersion(product.getVersion() + 1);
        return productMapper.updateByExampleSelective(product, example);
    }

    public void updateByPrimaryKey(Product product) {
        productMapper.updateByPrimaryKeySelective(product);
    }

    public List<Product> selectProductBySupplierId(Integer id) {
        ProductExample example = new ProductExample();
        example.createCriteria().andSupplierIdEqualTo(id);
        return productMapper.selectByExampleWithBLOBs(example);
    }

    public List<Product> selectProductByName(String name, Integer orderType, String sequence) {
        ProductCategory productCategory = productAttrService.selectProductCategoryByName("培训券");
        ProductExample example = new ProductExample();
        if (orderType == null) {
            example.setOrderByClause("priority desc,create_time asc");
        } else if (orderType == 1) {
            example.setOrderByClause("integral " + sequence);
        } else if (orderType == 2) {
            example.setOrderByClause("price " + sequence);
        }
        example.createCriteria().andIsShelfEqualTo(YesOrNo.YES).andCategoryIdNotEqualTo(productCategory.getId()).andNameLike(
                "%" + name + "%");
        return productMapper.selectByExampleWithBLOBs(example);
    }

    public Product selectProductAndChangeViewCount(Integer id) {
        Product product = selectProductByPrimaryKey(id);
        product.setViewCount(product.getViewCount() + 1);
        productMapper.updateByPrimaryKeySelective(product);
        asyncService.hit(id + "");
        return product;
    }
}
